#include "MatrixKeyBoard.h"
#include "DelayTime.h"
#include "LCD1602.h"

int GetKeyBoard()
{
	int key_num = 0;
	P1 = 0xff;
	// 按列读取
	P1_3 = 0;
	// 不能直接返回，直接返回，下次没按，直接返回0，造成屏幕上什么也没显示
	if(P1_7 == 0)       {Delayxms(20);while(P1_7 == 0);Delayxms(20);key_num = 1;}
	else if(P1_6 == 0)	{Delayxms(20);while(P1_6 == 0);Delayxms(20);key_num = 5;}
	else if(P1_5 == 0)  {Delayxms(20);while(P1_5 == 0);Delayxms(20);key_num = 9;}
	else if(P1_4 == 0)  {Delayxms(20);while(P1_4 == 0);Delayxms(20);key_num = 13;}

	P1 = 0xff;
	P1_2 = 0;
	if(P1_7 == 0) 		{Delayxms(20);while(P1_7 == 0);Delayxms(20);key_num = 2;}
	else if(P1_6 == 0)	{Delayxms(20);while(P1_6 == 0);Delayxms(20);key_num = 6;}	
	else if(P1_5 == 0)	{Delayxms(20);while(P1_5 == 0);Delayxms(20);key_num = 10;}
	else if(P1_4 == 0)	{Delayxms(20);while(P1_4 == 0);Delayxms(20);key_num = 14;}

	
	
	P1 = 0xff;
	P1_1 = 0;
	if(P1_7 == 0)		{Delayxms(20);while(P1_7 == 0);Delayxms(20);key_num = 3;}
	else if(P1_6 == 0)	{Delayxms(20);while(P1_6 == 0);Delayxms(20);key_num = 7;}	
	else if(P1_5 == 0)	{Delayxms(20);while(P1_5 == 0);Delayxms(20);key_num = 11;}
	else if(P1_4 == 0)	{Delayxms(20);while(P1_4 == 0);Delayxms(20);key_num = 15;}
	

	
	P1 = 0xff;
	P1_0 = 0;
	if(P1_7 == 0)		{Delayxms(20);while(P1_7 == 0);Delayxms(20);key_num = 4;}
	else if(P1_6 == 0)	{Delayxms(20);while(P1_6 == 0);Delayxms(20);key_num = 8;}	
	else if(P1_5 == 0)	{Delayxms(20);while(P1_5 == 0);Delayxms(20);key_num = 12;}
	else if(P1_4 == 0)	{Delayxms(20);while(P1_4 == 0);Delayxms(20);key_num = 16;}
	
	return key_num;
}